package com.fenxiangle.yueding.feature.publish.view;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.entity.bo.TabEntity;
import com.fenxiangle.yueding.entity.bo.UnMsgBo;
import com.fenxiangle.yueding.entity.bo.UnReadCountBo;
import com.fenxiangle.yueding.feature.mine.view.FragmentPublishDemand;
import com.fenxiangle.yueding.feature.mine.view.FragmentPublishOrder;
import com.fenxiangle.yueding.feature.mine.view.adapter.PublishDemandAdapter;
import com.fenxiangle.yueding.framework.api.HomeApi;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.flyco.tablayout.utils.UnreadMsgUtils;
import com.flyco.tablayout.widget.MsgView;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class MyPublishActivity extends BaseActivity implements FragmentPublishDemand.CallBackValue,FragmentPublishOrder.CallBackValue{


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tl_6)
    SlidingTabLayout mTl6;

    @BindView(R.id.viewpager)
    ViewPager mViewpager;
    private PublishDemandAdapter mPublishAdapter;
    private String[] mTitles = {"发布的需求", "发布的订单"};
    private ArrayList<CustomTabEntity> mTabEntities;
    private ArrayList<Fragment> mFragments;

    private String mPayNo;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_my_publish;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initView() {
        mFragments = new ArrayList<>();
        initToolBar(mToolbar, "我的发布", true, true);
        mTabEntities = new ArrayList<>();
        mFragments.add(new FragmentPublishDemand());
        mFragments.add(new FragmentPublishOrder());
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }
        mTl6.setViewPager(mViewpager, mTitles, this, mFragments);
        mTl6.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                getMsgCount();
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        mTl6.setCurrentTab(0);
    }

    @Override
    protected void onStart() {
        super.onStart();
        getMsgCount();
    }

    @Override
    protected void initData() {

    }


    protected int dp2px(float dp) {
        final float scale = this.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    private void getMsgCount() {
        AM.api().createApiService(HomeApi.class)
                .selectDiffMessageCount(new UnMsgBo(4 + ""))
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<UnReadCountBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UnReadCountBo msgBos) {
                        if (msgBos != null) {
                            if (msgBos.getTotalMyPublishLeft() > 0) {
                                //三位数
                                mTl6.showMsg(0, msgBos.getTotalMyPublishLeft());
                                mTl6.setMsgMargin(0, 54, 5);

                                MsgView rtv_2_4 = mTl6.getMsgView(0);
                                if (rtv_2_4 != null) {
                                    rtv_2_4.setStrokeColor(getResources().getColor(R.color.yuffff4b28));
                                    rtv_2_4.setBackgroundColor(getResources().getColor(R.color.yuffff4b28));
                                    rtv_2_4.setTextColor(getResources().getColor(R.color.bg_060000));
                                    rtv_2_4.setTextSize(10);
                                    DisplayMetrics dm = rtv_2_4.getResources().getDisplayMetrics();
                                    int size = (int) (14 * dm.density);
                                    UnreadMsgUtils.setSize(rtv_2_4, size);
                                }
                            }else {
                                mTl6.hideMsg(0);
                            }
                            if (msgBos.getTotalMyPublishRight() > 0) {
                                //设置未读消息红点
                                mTl6.showDot(1);
                                mTl6.setMsgMargin(1, 55, 5);
                                MsgView rtv_2_2 = mTl6.getMsgView(1);
                                if (rtv_2_2 != null) {
                                    UnreadMsgUtils.setSize(rtv_2_2, dp2px(7.5f));
                                }
                            }else {
                                mTl6.hideMsg(1);
                            }

                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void selectCounts(boolean isSelect) {
        if (isSelect){
            getMsgCount();
        }
    }

    @Override
    public void selectCount(boolean isSelect) {
        if (isSelect){
            getMsgCount();
        }
    }
}
